'use strict'

const { DataTypes, Model, Op } = require('../../../azeroth').sequelize;
class Job_1 extends Model { }

const initJobData_1 = (sequelize_aim) => {
  Job_1.init(
    {
      uid: {
        autoIncrement: true,
        type: DataTypes.INTEGER,
        allowNull: false,
        primaryKey: true
      },
      jobName: {
        type: DataTypes.STRING(128),
        allowNull: true
      },
      companyName: {
        type: DataTypes.STRING(128),
        allowNull: true
      },
      materialType: {
        type: DataTypes.STRING(128),
        allowNull: true
      },
      orderNumber: {
        type: DataTypes.STRING(128),
        allowNull: true
      },
      doffMax: {
        type: DataTypes.INTEGER,
        allowNull: true
      },
      startDoffSerialNumber: {
        type: DataTypes.INTEGER,
        allowNull: true
      },
      doffSum: {
        type: DataTypes.INTEGER,
        allowNull: true
      },
      lastPosition: {
        type: DataTypes.DECIMAL(16, 3),
        allowNull: true
      },
      status: {
        type: DataTypes.INTEGER,
        allowNull: true
      },
      createTime: {
        type: DataTypes.BIGINT,
        allowNull: true
      },
      finishTime: {
        type: DataTypes.BIGINT,
        allowNull: true
      },
      lastFlawClass: {
        type: DataTypes.INTEGER,
        allowNull: true
      },
      lastLaneInfo: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      lastDoffInfo: {
        type: DataTypes.STRING(1024),
        allowNull: true
      },
      comment: {
        type: DataTypes.STRING(1024),
        allowNull: true
      },
      exField: {
        type: DataTypes.STRING(1024),
        allowNull: true
      },
      exConfig1: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      exConfig2: {
        type: DataTypes.TEXT,
        allowNull: true
      }
    },
    {
      freezeTableName: true,
      sequelize: sequelize_aim,
      modelName: 'job',
      timestamps: false
    }
  )
}

Job_1.getMaxUid = async (sequelize_aim) => {
  let res = await sequelize_aim.query(`select max(uid) as uid from job`, { type: sequelize_aim.QueryTypes.SELECT });
  if (res.length) {
    return res[0].uid;
  } else {
    return 0;
  }
}

Job_1.upload = async (data) => {
  let res = await Job_1.bulkCreate([data], {
    updateOnDuplicate: ['uid', 'jobName', 'companyName', 'materialType', 'orderNumber', 'doffSum', 'createTime', 'finishTime', 'comment', 'exField', 'exConfig1', 'exConfig2']
  });
  return res;
}

module.exports = {
  Job_1,
  initJobData_1
};